package com.ht.controller;

import com.ht.data.Result;
import com.ht.domain.po.TbSchedule;
import com.ht.em.ResultEnum;
import com.ht.service.ScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@RestController
@RequestMapping("schedule")
@Api
@RefreshScope //开启自动刷新配置
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 添加医生排班 排班日期必须在十五天之内
     * @param doctorId
     * @param schedule
     * @return
     */
    @ApiOperation("添加医生排班")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId",value = "医生id",required = true,paramType = "path",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "schedule",value = "排班表实体",required = true,paramType = "body",dataTypeClass = TbSchedule.class)
    })
    @PostMapping("addSchedule/{doctorId}")
    public Result addSchedule(@PathVariable("doctorId")Long doctorId, @RequestBody TbSchedule schedule){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        Date date = null;
        try {
            date = sdf.parse(format);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH,15);
            //十五天后的日期
            String s = sdf.format(calendar.getTime());
            //前台传递日期
            String workDate = schedule.getWorkDate();
            Date parse = sdf.parse(workDate);
            //判断日期是否在十五天之后
            if (parse.compareTo(sdf.parse(s)) > 0){
                return Result.response(ResultEnum.EXCEED_MAXVALUE);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = scheduleService.addSchedule(doctorId,schedule);
        if (i <= 0){
            return Result.ERROR();
        }
        if (i == 2){
            return Result.response(ResultEnum.EXISTING_SCHEDULE);
        }
        return Result.OK();
    }

    /**
     * 删除医生排班
     * @param doctorId
     * @param dateStatus
     * @param workDate
     * @return
     */
    @ApiOperation("删除医生排班")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId",value = "医生id",required = true,paramType = "path",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateStatus",value = "排班时刻 0/1/2 上午/下午/晚上",required = true,paramType = "query",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "workDate",value = "坐诊日期",required = true,paramType = "query",dataTypeClass = String.class)
    })
    @DeleteMapping("deleteScheduleByDoctorId/{doctorId}")
    public Result deleteScheduleByDoctorId(@PathVariable("doctorId")Long doctorId,@RequestParam("dateStatus")Integer dateStatus,@RequestParam("workDate")String workDate){
        return scheduleService.deleteScheduleByDoctorId(doctorId,dateStatus,workDate);
    }
}
